//
//  WeaponPicViewController.m
//  MilitaryDemo
//
//  Created by qianfeng01 on 15-7-8.
//  Copyright (c) 2015年 qianfeng01. All rights reserved.
//

#import "WeaponPicViewController.h"
#import "WeaponCell2.h"
#import "WeaponDatabase.h"
#import "FirstModel.h"
#import "UIImageView+WebCache.h"
#import "WeaponDetailViewController.h"
@interface WeaponPicViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)UICollectionView *collectionView;


@end

@implementation WeaponPicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // self.view.backgroundColor=[UIColor whiteColor];
    [self creatCollectionView];
    [self initModel];
}
-(void)initModel
{
    self.dataArr=[[WeaponDatabase sharedManager]selectAllatDes:self.des];
    [self.collectionView reloadData];
}

-(void)creatCollectionView
{
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc]init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    // 对于垂直滚动，上下cell的间距，对于水平滚动，左右cell的间距 最小间距
    flowLayout.minimumLineSpacing = 10;
    // 设置item之间间距，实际间隔需要根据item的size和edgesInsets的值来调节
    flowLayout.minimumInteritemSpacing = 3;
    // 全局设置itemSize
    flowLayout.itemSize = CGSizeMake(2*(kScreenSize.width/3), 150);
    
    // 内边距
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(20, 0, kScreenSize.width-40, kScreenSize.height-64) collectionViewLayout:flowLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    self.collectionView.backgroundColor=[UIColor whiteColor];
    [_collectionView registerNib:[UINib nibWithNibName:@"WeaponCell2" bundle:nil] forCellWithReuseIdentifier:@"WeaponCell2"];
    [self.view addSubview:_collectionView];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WeaponCell2 *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"WeaponCell2" forIndexPath:indexPath];
    FirstModel *model=self.dataArr[indexPath.row];
//    NSString *str=[NSString stringWithFormat:@"%@.jpg",model.des];
    [cell.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.pic]placeholderImage:[UIImage imageNamed:@"zhanwei4"]];
    cell.titleLabel.text=model.title;
    return cell;

}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    FirstModel *model=self.dataArr[indexPath.row];
    WeaponDetailViewController *controller=[[WeaponDetailViewController alloc]init];
    controller.id=model.id;
    controller.title=model.title;
    //controller.image=model.pic;
    controller.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:controller animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
